package com.digitaldreamers.spaceshooter.effect;

import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.entity.Entity;
import org.andengine.entity.particle.BatchedSpriteParticleSystem;
import org.andengine.entity.particle.emitter.PointParticleEmitter;
import org.andengine.entity.particle.initializer.BlendFunctionParticleInitializer;
import org.andengine.entity.particle.initializer.ExpireParticleInitializer;
import org.andengine.entity.particle.modifier.ScaleParticleModifier;
import org.andengine.entity.sprite.UncoloredSprite;
import org.andengine.opengl.texture.region.ITextureRegion;

import android.opengl.GLES20;

import com.digitaldreamers.framework.BaseResourceManager;
import com.digitaldreamers.spaceshooter.Constants;

public class BeamEffect extends Entity {
	private BatchedSpriteParticleSystem mParticleSystem;
	private PointParticleEmitter mEmitter;
	private ITextureRegion mBeamTextureRegion;
	private Entity mParentEntity;
	private float mSecondsElapsed;
	
	@SuppressWarnings("deprecation")
	public BeamEffect(ITextureRegion beamTextureRegion, Entity parentEntity) {
		this.mBeamTextureRegion = beamTextureRegion;
		this.mParentEntity = parentEntity;
		this.setSize(this.mParentEntity.getWidthScaled(), this.mParentEntity.getHeightScaled());
		
		this.state1();
		this.state2();
		this.state3();
		
		this.attachChild(this.mParticleSystem);
		this.mParentEntity.attachChild(this);
		this.mParticleSystem.registerUpdateHandler(new IUpdateHandler() {
			@Override
			public void onUpdate(float secondsElapsed) {
				BeamEffect.this.mSecondsElapsed += secondsElapsed;
				if(BeamEffect.this.mSecondsElapsed > Constants.BEAM_EFFECT__LIFE_TIME + 1f) {
					BeamEffect.this.detachSelf();
				}
			}
			
			@Override
			public void reset() {
				
			}
		});
	}
	
	private void state1() {
		this.mEmitter = new PointParticleEmitter(this.getWidth() / 2, this.getHeight() / 2);
		this.mParticleSystem = new BatchedSpriteParticleSystem(this.mEmitter, Constants.BEAM_EFFECT__RATE_MINIMUM, Constants.BEAM_EFFECT__RATE_MAXIMUM, Constants.BEAM_EFFECT__PARTICLES_MAXIMUM, this.mBeamTextureRegion, BaseResourceManager.getInstance().mEngine.getVertexBufferObjectManager());
		this.mParticleSystem.addParticleInitializer(new ExpireParticleInitializer<UncoloredSprite>(Constants.BEAM_EFFECT__LIFE_TIME));
		this.mParticleSystem.addParticleInitializer(new BlendFunctionParticleInitializer<UncoloredSprite>(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA));
	}
	
	private void state2() {
		MyScaleParticleModifier<UncoloredSprite> myScaleParticleModifier = new MyScaleParticleModifier<UncoloredSprite>(Constants.BEAM_EFFECT__STATE_2_START_SECONDS, Constants.BEAM_EFFECT__STATE_2_END_SECONDS, Constants.BEAM_EFFECT__STATE_2_FROM_SCALE_X, Constants.BEAM_EFFECT__STATE_2_TO_SCALE_X, Constants.BEAM_EFFECT__STATE_2_FROM_SCALE_Y, Constants.BEAM_EFFECT__STATE_2_TO_SCALE_Y);
		myScaleParticleModifier.setParticleScaleCenterY(0);
		this.mParticleSystem.addParticleModifier(myScaleParticleModifier);
	}
	
	private void state3() {
		ScaleParticleModifier<UncoloredSprite> scaleParticleModifier = new ScaleParticleModifier<UncoloredSprite>(Constants.BEAM_EFFECT__STATE_3_START_SECONDS, Constants.BEAM_EFFECT__STATE_3_END_SECONDS, Constants.BEAM_EFFECT__STATE_3_FROM_SCALE_X, Constants.BEAM_EFFECT__STATE_3_TO_SCALE_X, Constants.BEAM_EFFECT__STATE_3_FROM_SCALE_Y, Constants.BEAM_EFFECT__STATE_3_TO_SCALE_Y);
		this.mParticleSystem.addParticleModifier(scaleParticleModifier);
	}
}
